import Taro from '@tarojs/taro'
import { stringify } from 'qs';
import { prefix } from '../utils'

export function add(data, callback) {
    return () => {
        const userInfo = Taro.getStorageSync('userInfo')
        const payload = {...data, ...userInfo}

        Taro.request({
            url: `${prefix}/api/v1/goods`,
            data: payload,
            method: 'POST',
            header: { 'content-type': 'application/json' }
        })
        .then(results => {
            const { data = {} } = results || {}
            if(callback) callback(data)
        })

    }
}

export function queryList(params = {}) {
    return dispatch => {

        Taro.request({
            url: `${prefix}/api/v1/goods?${stringify(params)}`,
            method: 'GET',
            header: { 'content-type': 'application/json' }
        })
        .then(results => {
            const { data = {} } = results || {}
            dispatch({
                type: 'goods/list',
                payload: { data, isLoading: false, isLoadMore: false, }
            })
        })

    }
}

export function queryListMore(params = {}) {
    return dispatch => {

        Taro.request({
            url: `${prefix}/api/v1/goods?${stringify(params)}`,
            method: 'GET',
            header: { 'content-type': 'application/json' }
        })
        .then(results => {
            const { data = {} } = results || {}
            dispatch({
                type: 'goods/listMore',
                payload: { dataMore: data, isLoading: false, isLoadMore: false, }
            })
        })

    }
}

export function queryProfile(params, callback) {
    return dispatch => {

        Taro.request({
            url: `${prefix}/api/v1/goods/${params.id}`,
            method: 'GET',
            header: { 'content-type': 'application/json' }
        })
        .then(results => {
            const { data = {} } = results || {}
            dispatch({
                type: 'goods/profile',
                payload: { profile: data, isLoading: false, isLoadMore: false, }
            })
            if(callback) callback(data)
        })

    }
}

export function queryUsers({id, ...params}, callback) {
    return dispatch => {

        Taro.request({
            url: `${prefix}/api/v1/goods/${id}/users?${stringify(params)}`,
            method: 'GET',
            header: { 'content-type': 'application/json' }
        })
        .then(results => {
            const { data = {} } = results || {}
            dispatch({
                type: 'goods/users',
                payload: { users: data }
            })
            if(callback) callback(data)
        })
        .catch(e=> callback(e))

    }
}

export function queryIntegrals(params, callback) {
    return dispatch => {
        Taro.request({
            url: `${prefix}/api/v1/goods/${params.id}/integrals`,
            method: 'GET',
            header: { 'content-type': 'application/json' }
        })
        .then(results => {
            const { data = {} } = results || {}
            dispatch({
                type: 'goods/integrals',
                payload: { integrals: data }
            })
            if(callback) callback(data)
        })

    }
}

export function createProfile(params, callback) {
    return dispatch => {
        const userInfo = Taro.getStorageSync('userInfo')

        Taro.request({
            url: `${prefix}/api/v1/users/${userInfo.userId}/goods`,
            method: 'POST',
            header: { 'content-type': 'application/json' },
            data: {userId: userInfo.userId, goodsId: params.id, goodsPrizeId: params.goodsPrizeId}
        })
        .then(results => {
            const { data = {} } = results || {}
            dispatch({
                type: 'goods/integralIns',
                payload: { integralIns: data }
            })
            if(callback) callback(data)
        })

    }
}